/**
 * Created by Jane-M on 2017/4/5.
 */


;(function ($) {
    $.fn.myScrollbar = function (options) {

        if (methods[options]) {
            return methods[options].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof options === 'object' || !options) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method' + options + 'does not exist on jQuery.tooltip');
        }

    },
        $.fn.myScrollbar.defaults = {
            scrollbarWidth: 6,//滚动条宽
            scrollPageY: 0,
            scrollPageX: 0,
            scrollY: 0,
            scrollbarClassName: 'scrollbar',//滚动条className
            scrollbarIndexClassName: 'scrollbarIndex'//索引条className
        }

    var _private = {
        //计算滚动条位置
        scrollResetSize: function (o) {
            var o = o.data !=undefined ? o.data.obj : o ;
            if (o.scrollHeight <= o.clientHeight) {
                o.scrollTop = 0;
                o.scrollBar.css('display', 'none');
            } else {
                o.scrollBar.css('display', 'inline-block');
            };
            var x = 0, y = 0;
            var p = o;
            while (p) {
                x += p.offsetLeft;
                y += p.offsetTop;
                p = p.offsetParent;
            };
            //var borderTop = parseInt(o.style.borderTopWidth || 0);
            //var borderBottom = parseInt(o.style.borderBottomWidth || 0);
            //o.scrollBar.css({'width': o.scrollBarWidth + 'px', 'height': o.clientHeight + 'px','top':y + borderTop + 'px','left': x + o.offsetWidth - o.scrollBarWidth + 'px'});
            o.scrollBar.css({'width': o.scrollBarWidth + 'px', 'height': o.clientHeight + 'px'});
            o.scrollBarIndex.css({'width': o.scrollBarWidth + 'px'});
            var h = o.clientHeight - (o.scrollHeight - o.clientHeight);

            //当滚动条滑块最小20个像素
            if (h < 20) {
                h = 20;
            };
            o.scrollBarHeight = h;
            o.scrollBarIndex.css({'left': '0px', 'height': h + 'px'});
            _private.setScrollPosition(o);
        },
        //设置索引条位置
        setScrollPosition: function (o) {
            o.scrollBarIndex.css('top', (o.clientHeight - o.scrollBarHeight) * o.scrollTop / (o.scrollHeight - o.clientHeight) + 'px');
        },

        //鼠标滚轮滚动
        scrollMove: function (event) {
            var div =  this;
            if (div.scrollHeight <= div.clientHeight) return true;
            var step = 20;
            if (event.originalEvent.wheelDelta < 0||event.originalEvent.detail>0) {
                if (div.scrollTop >= (div.scrollHeight - div.clientHeight)) return true;
                div.scrollTop += step;
            } else {
                if (div.scrollTop == 0) return true;
                div.scrollTop -= step;
            }
            _private.setScrollPosition(div);
            return false;
        },

        //拖动滚动条滚动
        myScrollbarMousedown:function (event) {
            var self = event.data.obj
            self.scrollPageY = event.clientY;
            self.scrollY = self.scrollTop;
            //isScrollMove = true;
            $('body').on('selectstart', function () {
                return false
            });
            self.scrollMoveObj = self;
            if (self.scrollMoveObj.scrollBar.className == '') {
                this.scrollDiv.scrollBarIndex.css('backgroundColor', '#fcd846');
            }
            return false;
        },

        //mouseup事件
        myScrollbarMouseup:function (event) {
            var self = event.data.obj;
            if (!self.scrollMoveObj)return;
            if (self.scrollBar.className == '') {
                self.scrollMoveObj.css('backgroundColor', '#fcd846');
            }
            self.scrollMoveObj = null;
            $('body').on('selectstart', function () {
                return true
            });
        },

        //mousemove事件
        myScrollbarMousemove:function (event) {
            var self = event.data.obj;
            if (!self.scrollMoveObj)return;
            var per = (self.scrollMoveObj.scrollHeight - self.scrollMoveObj.clientHeight) / (self.scrollMoveObj.clientHeight - self.scrollMoveObj.scrollBarHeight)
            self.scrollMoveObj.scrollTop = self.scrollY - (self.scrollPageY - event.clientY) * per;
            _private.setScrollPosition(self.scrollMoveObj);
        }
    };

    var methods = {
        init: function (option) {
            var self = this[0];
            var options = $.extend({}, $.fn.myScrollbar.defaults, option);
            //当内容未超出现在高度时，不添加滚动条
            if (!self || self.scrollHeight <= self.clientHeight || self.clientHeight == 0) {
                return;
            };

            //初始化滚动条
            self.scrollBarWidth = options.scrollbarWidth ;
            this.css({'overflow': 'hidden', 'display': 'inline-block'});
            self.scrollBar = $('<div></div>');
            this.parent().append(self.scrollBar);
            self.scrollBarIndex = $('<div></div>');
            self.scrollBar.append(self.scrollBarIndex);
            self.scrollBar.css({'position': 'relative','display': 'inline-block','backgroundColor': '#e1e1e1'});
            self.scrollBarIndex.css({'position': 'absolute', 'backgroundColor': '#fcd846'});
            self.scrollBar.className = options.scrollbarClassName;
            self.scrollBar.addClass(options.scrollbarClassName);
            self.scrollBarIndex.addClass(options.scrollbarIndexClassName);
            _private.scrollResetSize(self);

            //使用鼠标滚轮滚动
            /*self.scrollBar.scrollDiv = self;
            self.scrollBarIndex.scrollDiv = self;*/
            this.on('mousewheel.myScrollbar DOMMouseScroll.myScrollbar',_private.scrollMove);
            //拖动滚动条滚动
            self.scrollBarIndex.on('mousedown',{'obj':self}, _private.myScrollbarMousedown);
            $(document).on('mouseup.myScrollbar',{'obj':self},_private.myScrollbarMouseup);
            $(document).on('mousemove.myScrollbar',{'obj':self},_private.myScrollbarMousemove);

            //当页面大小发生变化时，重新计算滚动条位置
           // $(window).on('resize.myScrollbar',{'obj':self},_private.scrollResetSize);
            return this;
        }
    };
})(jQuery);